﻿namespace Iesi.Collections
{
    using System;
    using System.Collections;

    [Serializable]
    public abstract class DictionarySet : Set
    {
        protected IDictionary InternalDictionary;
        private static readonly object PlaceholderObject = new object();

        protected DictionarySet()
        {
        }

        public override bool Add(object o)
        {
            if (this.InternalDictionary[o] != null)
            {
                return false;
            }
            this.InternalDictionary.Add(o, PlaceholderObject);
            return true;
        }

        public override bool AddAll(ICollection c)
        {
            bool flag = false;
            foreach (object obj2 in c)
            {
                flag |= this.Add(obj2);
            }
            return flag;
        }

        public override void Clear()
        {
            this.InternalDictionary.Clear();
        }

        public override bool Contains(object o)
        {
            return (this.InternalDictionary[o] != null);
        }

        public override bool ContainsAll(ICollection c)
        {
            foreach (object obj2 in c)
            {
                if (!this.Contains(obj2))
                {
                    return false;
                }
            }
            return true;
        }

        public override void CopyTo(Array array, int index)
        {
            this.InternalDictionary.Keys.CopyTo(array, index);
        }

        public override IEnumerator GetEnumerator()
        {
            return this.InternalDictionary.Keys.GetEnumerator();
        }

        public override bool Remove(object o)
        {
            bool flag = this.Contains(o);
            if (flag)
            {
                this.InternalDictionary.Remove(o);
            }
            return flag;
        }

        public override bool RemoveAll(ICollection c)
        {
            bool flag = false;
            foreach (object obj2 in c)
            {
                flag |= this.Remove(obj2);
            }
            return flag;
        }

        public override bool RetainAll(ICollection c)
        {
            Set set = new HybridSet(c);
            Set set2 = new HybridSet();
            foreach (object obj2 in this)
            {
                if (!set.Contains(obj2))
                {
                    set2.Add(obj2);
                }
            }
            return this.RemoveAll(set2);
        }

        public override int Count
        {
            get
            {
                return this.InternalDictionary.Count;
            }
        }

        public override bool IsEmpty
        {
            get
            {
                return (this.InternalDictionary.Count == 0);
            }
        }

        public override bool IsSynchronized
        {
            get
            {
                return false;
            }
        }

        protected object Placeholder
        {
            get
            {
                return PlaceholderObject;
            }
        }

        public override object SyncRoot
        {
            get
            {
                return this.InternalDictionary.SyncRoot;
            }
        }
    }
}

